package com.testing.base;

import com.testing.Utlis.ReadPropertiesUtil;
import com.testing.Utlis.ScreenshotUtil;
import com.testing.page.LoginPage;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class BaseTest {

    public WebDriver driver;

    // 缩放浏览器比例80%
    public String zoomOut= "document.body.style.zoom='0.8'";

    @BeforeMethod
    public WebDriver startBrowser(){
        // 启动浏览器
        BaseDriver baseDriver = new BaseDriver();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("lang=zh-CN");

        driver = baseDriver.startBrowserDriver(options);
        driver.get(ReadPropertiesUtil.readProperties(ReadPropertiesUtil.FILEPATH, "system_url"));
        driver.manage().window().maximize();
//        BasePageElement.executeJS(driver, zoomOut);
        driver.manage().window().setSize(new Dimension(2000, 1000));
        BasePageElement.latencyTime(2000);
        return driver;
    }

    @AfterMethod
    public void stopBrowser(){
        // ScreenshotUtil.capture(driver);  // 截图
        // driver.close: 关闭当前浏览器窗口,不清理后天进程；driver.quit: 可以确保清理所有资源，包括关闭所有窗口并结束 ChromeDriver 进程
        driver.quit();
    }

    public void login(){
        LoginPage loginPage = new LoginPage(driver);
        loginPage.languageSelection().click();
        loginPage.simplifiedChinese().click();
        loginPage.userAccount().sendKeys(ReadPropertiesUtil.readProperties(ReadPropertiesUtil.FILEPATH, "username"));
        loginPage.userPassword().sendKeys(ReadPropertiesUtil.readProperties(ReadPropertiesUtil.FILEPATH, "password"));
        loginPage.loginButton().click();
        BasePageElement.latencyTime(1000);
        driver.get(ReadPropertiesUtil.readProperties(ReadPropertiesUtil.FILEPATH, "systemIP")+"/owner/enterprise/information");
        BasePageElement.latencyTime(1000);
    }

    public void login(String username, String password){
        LoginPage loginPage = new LoginPage(driver);
        loginPage.languageSelection().click();
        loginPage.simplifiedChinese().click();
        loginPage.userAccount().sendKeys(username);
        loginPage.userPassword().sendKeys(password);
        loginPage.loginButton().click();
    }
}
